package com.ogilvy.audi.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

/**
 * This class is used to represent available roles in the database.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *         Version by Dan Kibler dan@getrolling.com
 *         Extended to implement Acegi GrantedAuthority interface
 *         by David Carter david@carter.net
 */
@Entity
@Table(name = "TBL_ROLE")
@NamedQueries({
        @NamedQuery(
                name = "findRoleByName",
                query = "select r from Role r where r.name = :name "
        )
})
public class Role extends BaseObject implements Serializable, GrantedAuthority,ConfigAttribute {
 private static final long serialVersionUID = 3690197650654049848L;
    private Long id;
    private String name;
    private String description;
    private String type;

    private String displayName;

   private Date createTime;

    private Integer version;


    // private Set<Menu> menus;

     private Set<Resource> resources;
    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Role() {
    }

    /**
     * Create a new instance and set the name.
     *
     * @param name name of the role.
     */
    public Role(final String name) {
        this.name = name;
    }

    @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
//    	@SequenceGenerator(name = "role_seq", sequenceName = "TBL_role_seq", allocationSize = 1)
//	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "role_seq")
    @Column(name="NUMID")
    public Long getId() {
        return id;
    }

    /**
     * @return the name property (getAuthority required by Acegi's GrantedAuthority interface)
     * @see org.springframework.security.core.GrantedAuthority#getAuthority()
     */
    @Transient
    public String getAuthority() {
        return getName();
    }

    @Column(length = 20,name="VC2NAME")
    public String getName() {
        return this.name;
    }
    @Column(length = 20,name="VC2TYPE")
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column(length = 64,name="VC2DESCRIPTION")
    public String getDescription() {
        return this.description;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    @Column(length = 200,name="VC2DISPLAYNAME")
    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

      @Column(name = "DATCREATETIME", length = 8)
	@Temporal(value= TemporalType.TIMESTAMP)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
   @Version
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Role)) {
            return false;
        }

        final Role role = (Role) o;

        return !(name != null ? !name.equals(role.name) : role.name != null);

    }

//       @JSON(serialize=false)
//   @ManyToMany(mappedBy = "roles",targetEntity = Menu.class, fetch = FetchType.EAGER)
//  public Set<Menu> getMenus() {
//        return menus;
//    }
//
//    public void setMenus(Set<Menu> menus) {
//        this.menus = menus;
//    }

      @JSON(serialize=false)
    @ManyToMany(mappedBy = "roles",targetEntity = com.ogilvy.audi.model.Resource.class, fetch = FetchType.EAGER,cascade = {CascadeType.MERGE,CascadeType.REFRESH,CascadeType.PERSIST})
   //   @ManyToMany(mappedBy="roles",cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
 //  @JoinTable(name = "TBL_REL_ROLE_RESOURCE", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "resource_id"))
//    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

    public Set<Resource> getResources() {
        return resources;
    }

    public void setResources(Set<Resource> resources) {
        this.resources = resources;
    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (name != null ? name.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.name)
                .toString();
    }

    /**
     * {@inheritDoc}
     */
    public int compareTo(Object o) {
        return (equals(o) ? 0 : -1);
    }
  @Transient
    public String getAttribute() {
        return name;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
